package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.constant.QualityErrorCode;
import com.ruibang.glass.quality.domain.req.DefectReq;
import com.ruibang.glass.quality.entity.Defect;
import com.ruibang.glass.quality.mapper.DefectMapper;
import com.ruibang.glass.quality.service.DefectService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-08-30  14:17
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class DefectServiceImpl extends ServiceImpl<DefectMapper, Defect> implements DefectService {


    /**
     * @Description: 查询缺陷
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 14:27
     */
    @Override
    public ResultBody queryDefect(DefectReq defectReq) {
        QueryWrapper<Defect> dQw = new QueryWrapper<>();
        dQw.lambda().like(Defect::getDefectName, defectReq.getDefectName());
        final IPage<Defect> page = this.page(new Page<>(defectReq.getPageNo(), defectReq.getPageSize()), dQw.lambda());
        return ResultBody.success(page);
    }


    /**
     * @Description: 根据缺陷组获取缺陷信息
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 15:00
     */
    @Override
    public List<Defect> getByDefectGroupId(List<String> defectGroupIds) {
        if (CollectionUtils.isNotEmpty(defectGroupIds)) {
            QueryWrapper<Defect> dQw = new QueryWrapper<>();
            dQw.lambda().in(Defect::getDefectGroupId, defectGroupIds);
            return this.list(dQw);
        }
        return Collections.emptyList();
    }


    /**
     * @Description: 新增缺陷
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 15:26
     */
    @Override
    public synchronized ResultBody insert(Defect defect) {
        try {
            //尺寸范围判断
            Double maxSize = defect.getMaxSize();
            Double minSize = defect.getMinSize();
            if (minSize > maxSize) {
                return ResultBody.fail(QualityErrorCode.SIZE_ERROR.getCode(), QualityErrorCode.SIZE_ERROR.getMsg());
            }
            //层深范围判断
            Integer maxLayerDepth = defect.getMaxLayerDepth();
            Integer minLayerDepth = defect.getMinLayerDepth();
            if (minLayerDepth > maxLayerDepth) {
                return ResultBody.fail(QualityErrorCode.LAYER_DEPTH_ERROR.getCode(), QualityErrorCode.LAYER_DEPTH_ERROR.getMsg());
            }
            //保存
            this.save(defect);
        } catch (Exception e) {
            return ResultBody.fail(QualityErrorCode.ERROR.getCode(), QualityErrorCode.ERROR.getMsg());
        }
        return ResultBody.success();
    }


    /**
     * @Description: 获取缺陷列表
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 15:30
     */
    @Override
    public ResultBody getDefectList() {
        return ResultBody.success(baseMapper.getDefectList());
    }


    /**
     * @Description: 根据缺陷组id获取缺陷
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 15:48
     */
    @Override
    public List<Defect> getByGroupId(String groupId) {
        if (StringUtils.isNotBlank(groupId)) {
            QueryWrapper<Defect> dQw = new QueryWrapper<>();
            dQw.lambda().eq(Defect::getDefectGroupId, groupId);
            return this.list(dQw);
        }
        return Collections.emptyList();
    }


    /**
     * @Description: 根据缺陷组id删除缺陷
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 16:25
     */
    @Override
    public void deleteByDefectGroupId(String defectGroupId) {
        QueryWrapper<Defect> dgQw = new QueryWrapper<>();
        dgQw.lambda().eq(Defect::getDefectGroupId, defectGroupId);
        this.remove(dgQw);
    }
}
